import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# 假设这是您的二维数组数据
data = np.array([[43, 295],
        [42, 294],
        [42, 291],
        [42, 288],
        [42, 286],
        [42, 282],
        [43, 277],
        [45, 270],
        [45, 266],
        [47, 260],
        [48, 248],
        [49, 239],
        [50, 232],
        [51, 225],
        [53, 217],
        [57, 207],
        [59, 205],
        [62, 199],
        [65, 196],
        [68, 192],
        [72, 188],
        [72, 186],
        [76, 183],
        [80, 179],
        [84, 174],
        [88, 170],
        [93, 166],
        [98, 162],
        [103, 158],
        [108, 153],
        [112, 150],
        [117, 146],
        [122, 142],
        [128, 138],
        [132, 136],
        [136, 133],
        [144, 129],
        [148, 126],
        [153, 123],
        [158, 121],
        [161, 119],
        [164, 118],
        [168, 117],
        [168, 116],
        [171, 115],
        [173, 114],
        [176, 114],
        [177, 113],
        [180, 112],
        [182, 111],
        [184, 111],
        [186, 111],
        [189, 111],
        [192, 111],
        [195, 111],
        [198, 111],
        [201, 111],
        [206, 111],
        [213, 111],
        [217, 111],
        [223, 111],
        [228, 111],
        [233, 111],
        [238, 112],
        [239, 112],
        [241, 113],
        [243, 114],
        [243, 115],
        [244, 116],
        [246, 117],
        [248, 118],
        [250, 120],
        [252, 122],
        [256, 125],
        [260, 130],
        [263, 134],
        [268, 138],
        [272, 145],
        [276, 148],
        [280, 154],
        [288, 163],
        [293, 170],
        [300, 180],
        [308, 190],
        [316, 202],
        [321, 210],
        [324, 214],
        [332, 225],
        [334, 230],
        [337, 235],
        [340, 240],
        [341, 242],
        [344, 246],
        [346, 250],
        [348, 254],
        [350, 259],
        [353, 264],
        [355, 267],
        [356, 271],
        [357, 274],
        [358, 277],
        [360, 280],
        [360, 282],
        [361, 283],
        [361, 284],
        [362, 286],
        [362, 286],
        [362, 287],
        [362, 286]])

data = data[::10]

# 提取x和y数据
x_data = data[:, 0]  # 第一列作为x坐标
y_data = data[:, 1]  # 第二列作为y坐标

# 定义正弦函数形式
def sin_func(x, A, w, phi, c):
    return A * np.sin(w * x + phi) + c



# 初始猜测值（A: 振幅, w: 角频率, phi: 相位, c: 垂直偏移）
initial_guess = [1, 0.5, 0, 0]

# 使用 curve_fit 进行拟合
params, params_covariance = curve_fit(sin_func, x_data, y_data, p0=initial_guess)

# 提取拟合参数
A, w, phi, c = params

# 打印拟合结果
print(f"拟合参数: 振幅(A)={A:.2f}, 角频率(w)={w:.2f}, 相位(phi)={phi:.2f}, 垂直偏移(c)={c:.2f}")

# 生成用于绘图的 x 轴数据
x_fit = np.linspace(min(x_data), max(x_data), 1000)
y_fit = sin_func(x_fit, A, w, phi, c)

# 绘制原始数据点
plt.scatter(x_data, y_data, label='Data Points')
# 绘制拟合曲线
plt.plot(x_fit, y_fit, 'r-', label=f'Fit: A={A:.2f}, w={w:.2f}, φ={phi:.2f}, c={c:.2f}')
# 添加图例
plt.legend()
# 设置图表标题和标签
plt.title('Sine Curve Fitting')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
# 显示图形
plt.show()